package com.engine.salary.service.impl;

import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.entity.taxagent.dto.TaxAgentManageRangeEmployeeListDTO;
import com.engine.salary.entity.taxagent.param.TaxAgentImportParam;
import com.engine.salary.entity.taxagent.param.TaxAgentRangeDownloadTemplateParam;
import com.engine.salary.entity.taxagent.po.TaxAgentPO;
import com.engine.salary.service.TaxAgentExcelService;
import com.engine.salary.service.TaxAgentService;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.excel.ExcelComment;
import com.engine.salary.util.excel.ExcelParseHelper;
import com.engine.salary.util.excel.ExcelUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.Validate;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import weaver.file.ImageFileManager;
import weaver.general.Util;
import weaver.hrm.User;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Harryxzy
 * @date 2023/01/05 15:11
 * @description
 */
public class TaxAgentExcelServiceImpl extends Service implements TaxAgentExcelService {

    public TaxAgentService getTaxAgentService(User user) {
        return ServiceUtil.getService(TaxAgentServiceImpl.class, user);
    }

    @Override
    public XSSFWorkbook downloadTemplateRange(TaxAgentRangeDownloadTemplateParam downloadTemplateParam, int currentEmployeeId) {
        // 没有传递TaxAgentId就是当前登录人能够导入自己所能管理的个税扣缴义务人
        TaxAgentPO taxAgent = downloadTemplateParam.getTaxAgentId() == null ? null : getTaxAgentService(user).getById(downloadTemplateParam.getTaxAgentId());
        //获取操作按钮资源
        List<List<Object>> rowList = getExcelRowList(taxAgent);
        // 注释
        List<ExcelComment> excelComments = Lists.newArrayList();
        excelComments.add(new ExcelComment(0, 0, 1, 2, SalaryI18nUtil.getI18nLabel(100344, "必填")));
        excelComments.add(new ExcelComment(5, 0, 7, 3, SalaryI18nUtil.getI18nLabel(100344, "若不填，默认全部员工状态。指定员工状态格式：试用、正式、临时、试用延期")));
        //获取excel
        return ExcelUtil.genWorkbookV2(rowList, "个税扣缴义务人人员范围", excelComments);
    }

    @Override
    public Map<String, Object> preview(TaxAgentImportParam importParam) {
        Map<String, Object> apidatas = new HashMap<String, Object>();

        //excel文件id
        String imageId = Util.null2String(importParam.getImageId());
        Validate.notBlank(imageId, "imageId为空");

        InputStream fileInputStream = null;
        try {
            fileInputStream = ImageFileManager.getInputStreamById(Integer.parseInt(imageId));
            List<TaxAgentManageRangeEmployeeListDTO>  taxAgentManageRangeEmployees = ExcelParseHelper.parse2Map(fileInputStream, TaxAgentManageRangeEmployeeListDTO.class, 0, 1, 6, "TaxAgentEmployee.xlsx");
            apidatas.put("preview", taxAgentManageRangeEmployees);
        } finally {
            IOUtils.closeQuietly(fileInputStream);
        }
        return apidatas;
    }

    /**
     * @description 获取excel数据行
     * @return List<List<String>>
     * @author Harryxzy
     * @date 2023/1/5 15:47
     */
    private List<List<Object>> getExcelRowList(TaxAgentPO taxAgent) {
        // 表头
        List<Object> headers = Lists.newArrayList();
        if (taxAgent == null) {
            headers.add(SalaryI18nUtil.getI18nLabel( 86184, "个税扣缴义务人"));
        }
        headers.add(SalaryI18nUtil.getI18nLabel( 85429, "姓名"));
        headers.add(SalaryI18nUtil.getI18nLabel( 86185, "部门"));
        headers.add(SalaryI18nUtil.getI18nLabel(86186, "手机号"));
        headers.add(SalaryI18nUtil.getI18nLabel(86317, "工号"));
        headers.add(SalaryI18nUtil.getI18nLabel(86317, "证件号码"));
        headers.add(SalaryI18nUtil.getI18nLabel(86318, "员工状态"));
        List<List<Object>> rowList = new ArrayList<>();
        rowList.add(headers);
        return rowList;
    }
}
